package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.bean.SelectPayMethod;
import com.stn.zjx.givingtreasure.event.SelectPayMethodEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SelectPayMethodItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<SelectPayMethod> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;
   public int position=0;
   public SelectPayMethodItemListAdapter(Context context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_select_pay_method, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       SelectPayMethod    selectPayMethod=dataBeanList.get(i);
         switch (selectPayMethod.getPayType()){
             case 0:
                  ih.pay_name.setText(context.getString(R.string.balance));
                 ih.iv.setImageDrawable(null);
                 break;
             case 1:
                 ih.pay_name.setText(context.getString(R.string.alipay));
                 ih.iv.setImageResource(R.mipmap.alipay_pay);
                 break;
             case 2:
                 ih.pay_name.setText(context.getString(R.string.wechat));
                 ih.iv.setImageResource(R.mipmap.wechat_pay);
                 break;
             case 3:
                 ih.iv.setImageDrawable(null);
                 ih.pay_name.setText(selectPayMethod.getUserBankCardName()+selectPayMethod.getUserBankCardNo()
                 .substring(selectPayMethod.getUserBankCardNo().length()-4,selectPayMethod.getUserBankCardNo().length()));
                 break;
         }
         if(position==i){
             ih.select_iv.setImageResource(R.drawable.language_radio_select);
         }else{
             ih.select_iv.setImageDrawable(null);
         }
         ih.rootView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                position=i;
                 SelectPayMethodEvent event=new SelectPayMethodEvent();
                 event.flag=SelectPayMethodEvent.SELECTFINISH;
                 event.setPayType(selectPayMethod.getPayType());
                 event.setUserBankCardName(selectPayMethod.getUserBankCardName());
                 event.setUserBankCardNo(selectPayMethod.getUserBankCardNo());
                 event.setUserBankCardType(selectPayMethod.getUserBankCardType());
                 EventBus.getDefault().post(event);
                 notifyDataSetChanged();
             }
         });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<SelectPayMethod> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<SelectPayMethod> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
        public ImageView iv;
        public TextView pay_name;
        public ImageView select_iv;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            iv=itemView.findViewById(R.id.iv);
            pay_name=itemView.findViewById(R.id.pay_name);
            select_iv=itemView.findViewById(R.id.select_iv);
        }
   }

}